<template>
  <div class="login">
    <h3 class="title">基础平台统一登陆页面</h3>
  </div>
</template>

<script>
  import {ssoLogin} from "@/api/login";
  import {setAccessToken, setToken} from '@/utils/auth'
  import constant from "@/constant";

  export default {
    name: "ssoLogin",
    data() {
      return {};
    },
    created() {
      this.checkHaveCode();
    },
    mounted() {
    },
    methods: {
      checkHaveCode() {
        let urlSearchParams = new URLSearchParams(window.location.search)
        let code = urlSearchParams.get('code');
        console.log(code)
        //如果没有拿到授权码，就打开统一登录页面
        if (!code) {
          //没登录，就去统一登录页面获取一次性使用的授权码
          window.location.href = constant.SSO_SERVER_WEB + "?appId=8272087603845607424&redirectUri=http://cool.client1.com:820/ssoLogin"
        } else {
          console.log("else")
          console.log(code)
          //用授权码，向自己后台进行请求登录
          ssoLogin(code).then((res) => {
            console.log(res)
            setToken(res.data.token)
            setAccessToken(res.data.accessToken.accessToken)
            this.$router.push({path: "/"})
          }).catch(() => {
            console.log("异常发生")
            //授权码可能过期,验证异常，再去统一登录页面获取一次性使用的授权码
            window.location.href = constant.SSO_SERVER_WEB + "?appId=8272087603845607424&redirectUri=http://cool.client1.com:820/ssoLogin"
          });
        }
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login-background.jpg");
    background-size: cover;
  }

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }
</style>
